<template>
  <div class="app-container">
    <ContentWrap>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <!--      <el-form-item label="设备归属水司" prop="tenantId">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.tenantId"-->
        <!--          placeholder="请输入设备归属水司"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--      <el-form-item label="层级编号" prop="AreaID">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.AreaID"-->
        <!--          placeholder="请输入层级编号"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <el-form-item label="户表类型" prop="easIon">
          <el-select v-model="queryParams.easIon" placeholder="请选择户表类型"
                     clearable @keyup.enter.native="handleQuery">
            <el-option label="远传表" :value="0"></el-option>
            <el-option label="机械表" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户编号" prop="terminalID">
          <el-input
            v-model="queryParams.terminalID"
            placeholder="请输入用户编号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName">
          <el-input
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="用户地址" prop="terminalAddress">
          <el-input
            v-model="queryParams.terminalAddress"
            placeholder="请输入用户地址"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <!--      <el-form-item label="用户状态(2-停用1-使用0-在档)" prop="TerminalMark">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.TerminalMark"-->
        <!--          placeholder="请输入用户状态(2-停用1-使用0-在档)"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--      <el-form-item label="口径" prop="caliber">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.caliber"-->
        <!--          placeholder="请输入口径"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--      <el-form-item label="抄表频次(0-每月抄表,1-单月抄表,2-双月抄表)" prop="meterReading">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.meterReading"-->
        <!--          placeholder="请输入抄表频次(0-每月抄表,1-单月抄表,2-双月抄表)"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--      <el-form-item label="远传表编号" prop="easIonID">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.easIonID"-->
        <!--          placeholder="请输入远传表编号"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <!--      <el-form-item label="远传表厂家编号" prop="supplierID">-->
        <!--        <el-input-->
        <!--          v-model="queryParams.supplierID"-->
        <!--          placeholder="请输入远传表厂家编号"-->
        <!--          clearable-->
        <!--          @keyup.enter.native="handleQuery"-->
        <!--        />-->
        <!--      </el-form-item>-->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['device:terminalsXbInfo:add']"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['device:terminalsXbInfo:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['device:terminalsXbInfo:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['device:terminalsXbInfo:export']"
          >导出
          </el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="terminalsXbInfoList" size="mini" ref="terminalsXbInfoTable"
                border @selection-change="handleSelectionChange" :height="getAvailableHeight">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column type="index" width="40" align="center" label="#" :fixed="true"/>
        <!--      <el-table-column label="平台自增主键" align="center" prop="id"/>-->
        <!--      <el-table-column label="设备归属水司" align="center" prop="tenantId"/>-->
        <!--      <el-table-column label="层级编号" align="center" prop="AreaID"/>-->
        <el-table-column label="户表类型" align="center" prop="easIon" width="120">
          <template slot-scope="scope">
          <span>
            {{ ['远传表', '机械表'][scope.row.easIon] }}
          </span>
          </template>
        </el-table-column>
        <el-table-column label="用户编号" align="center" prop="terminalID" width="150"/>
        <el-table-column label="设备编号" align="center" prop="easIonID" width="120">
          <template slot-scope="scope">
          <span v-if="scope.row.easIon===1">
            /
          </span>
            <span v-else>
            {{ scope.row.easIonID }}
          </span>
          </template>
        </el-table-column>
        <el-table-column label="用户名称" align="left" prop="userName"/>
        <el-table-column label="用户地址" align="left" prop="terminalAddress"/>
        <!--      <el-table-column label="用户状态(2-停用1-使用0-在档)" align="center" prop="TerminalMark"/>-->
        <!-- <el-table-column label="远传表编号" align="center" prop="easIonID" width="150">
          <template slot-scope="scope">
          <span v-if="scope.row.easIon===1">
            /
          </span>
            <span v-else>
            {{ scope.row.easIonID.substr(10) }}
          </span>
          </template>
        </el-table-column> -->
        <el-table-column label="生产厂家" align="center" prop="supplierID" width="100">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.terminal_supplier" :value="scope.row.supplierID"/>
          </template>
        </el-table-column>
        <el-table-column label="口径" align="center" prop="caliber" width="80"/>
        <el-table-column label="抄表频次" align="center" prop="meterReading" width="100">
          <template slot-scope="scope">
          <span>
            {{ ['每月抄表', '单月抄表', '双月抄表'][scope.row.meterReading] }}
          </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['device:terminalsXbInfo:edit']"
            >修改
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['device:terminalsXbInfo:remove']"
            >删除
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-refresh"
              @click="handleXbReplace(scope.row)"
              v-hasPermi="['device:terminalsXbInfo:remove']"
            >换表
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        :total="total"
        :page-sizes="[15, 30, 45, 60]"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改户表档案对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" :close-on-click-modal="false" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <!--        <el-form-item label="设备归属水司" prop="tenantId">-->
          <!--          <el-input v-model="form.tenantId" placeholder="请输入设备归属水司"/>-->
          <!--        </el-form-item>-->
          <!--        <el-form-item label="层级编号" prop="AreaID">-->
          <!--          <el-input v-model="form.AreaID" placeholder="请输入层级编号"/>-->
          <!--        </el-form-item>-->
          <el-form-item label="户表类型" prop="easIon">
            <el-select v-model="form.easIon" placeholder="请选择户表类型" style="width: 100%;"
                       @change="handleEasionChange">
              <el-option label="远传表" :value="0"></el-option>
              <el-option label="机械表" :value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="用户编号" prop="terminalID">
            <el-input v-model="form.terminalID" placeholder="请输入用户编号"/>
          </el-form-item>
          <el-form-item label="用户名称" prop="userName">
            <el-input v-model="form.userName" placeholder="请输入用户名称"/>
          </el-form-item>
          <el-form-item label="用户地址" prop="terminalAddress">
            <el-input v-model="form.terminalAddress" placeholder="请输入用户地址"/>
          </el-form-item>
          <el-form-item label="远传表编号" prop="easIonID" v-if="form.easIon===0">
            <el-input v-model="form.easIonID" placeholder="请输入远传表编号"/>
          </el-form-item>
          <el-form-item v-if="form.easIon===0" label="生产厂家" prop="supplierID">
            <el-select v-model="form.supplierID" placeholder="请选择生产厂家" style="width: 100%;">
              <el-option v-for="dict in dict.type.terminal_supplier"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
          <el-form-item v-else label="生产厂家" prop="supplierID">
            <el-select v-model="form.supplierID" placeholder="请选择生产厂家" style="width: 100%;">
              <el-option v-for="dict in dict.type.terminal_supplier"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
          <!--        <el-form-item label="用户状态(2-停用1-使用0-在档)" prop="TerminalMark">-->
          <!--          <el-input v-model="form.TerminalMark" placeholder="请输入用户状态(2-停用1-使用0-在档)"/>-->
          <!--        </el-form-item>-->
          <el-form-item label="口径" prop="caliber">
            <el-select v-model="form.caliber" placeholder="请选择口径" style="width: 100%;">
              <el-option label="15" :value="15"></el-option>
              <el-option label="20" :value="20"></el-option>
              <el-option label="25" :value="25"></el-option>
              <el-option label="30" :value="30"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="抄表频次" prop="meterReading">
            <el-select v-model="form.meterReading" placeholder="请选择抄表频次" style="width: 100%;">
              <el-option label="每月抄表" :value="0"></el-option>
              <el-option label="单月抄表" :value="1"></el-option>
              <el-option label="双月抄表" :value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

      <DeviceReplace ref="refXbDeviceReplace" :callback="getList"/>
    </ContentWrap>
  </div>
</template>

<script>
import {
  listTerminalsXbInfo,
  getTerminalsXbInfo,
  delTerminalsXbInfo,
  addTerminalsXbInfo,
  updateTerminalsXbInfo
} from "@/api/leakage/terminalsXbInfo";
import DeviceReplace from '../../components/DeviceReplace'

export default {
  name: "TerminalsXbInfo",
  components: {
    DeviceReplace
  },
  dicts: ['terminal_supplier'],
  data() {
    return {
      screenHeight: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 户表档案表格数据
      terminalsXbInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
        tenantId: null,
        terminalID: null,
        terminalAddress: null,
        caliber: null,
        userName: null,
        meterReading: null,
        easIon: null,
        easIonID: null,
        supplierID: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        easIon: [
          {required: true, message: "请选择户表类型", trigger: "blur"}
        ],
        terminalID: [
          {required: true, message: "用户编号不能为空", trigger: "blur"}
        ],
        easIonID: [
          {required: true, message: "请输入远传表编号", trigger: "blur"}
        ],
        supplierID: [
          {required: true, message: "请选择生产厂家", trigger: "blur"}
        ],
        meterReading: [
          {required: true, message: "请选择抄表频次", trigger: "blur"}
        ],
      }
    };
  },
  watch: {
    'form.easIon' (val) {
        if(val == 0) {
          this.$set(this.rules.supplierID[0], 'required', true)
          // 手动触发验证（可选）
          // this.$nextTick(() => {
          //   	this.$refs.form.validateField('phone');
          // });
        }else {
          this.$set(this.rules.supplierID[0], 'required', false)
          // 手动触发验证（可选）
          // this.$nextTick(() => {
          //   this.$refs.form.validateField('phone');
          // });
        }  
    },
  },
  created() {
    this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - 236
    },
  },
  methods: {
    /** 查询户表档案列表 */
    getList() {
      this.loading = true;
      listTerminalsXbInfo(this.queryParams).then(response => {
        this.terminalsXbInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        AreaID: null,
        terminalID: null,
        terminalAddress: null,
        TerminalMark: null,
        caliber: null,
        userName: null,
        meterReading: null,
        easIon: null,
        easIonID: null,
        supplierID: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加户表档案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.reset();
      const id = row.id || this.ids
      getTerminalsXbInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改户表档案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.easIon === 0 && this.form.supplierID == undefined) {
            this.$message.error('请选择生产厂家')
            return
          }

          if (this.form.id != null) {
            updateTerminalsXbInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTerminalsXbInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal.confirm('是否确认删除户表档案编号为"' + IDs + '"的数据项？').then(function () {
        return delTerminalsXbInfo(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('device/terminalsXbInfo/export', {
        ...this.queryParams
      }, `terminalsXbInfo_${new Date().getTime()}.xlsx`)
    },
    handleEasionChange() {
      this.$refs.form.clearValidate()
    },
    handleXbReplace(row) {
      this.$refs.refXbDeviceReplace.openDialog(2, row)
    }
  }
};
</script>
